﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

/// <summary>
/// PasswordHelper 的摘要说明
/// </summary>
public class PasswordHelper
{
    public PasswordHelper()
    {
       
    }
    public static string md5Encoding(string password)
    {
        MD5 md5 = MD5.Create();
        byte[] buffer = Encoding.Default.GetBytes(password);
        byte[] md5buffer = md5.ComputeHash(buffer);
        string str = null;
        foreach (byte b in md5buffer)
        {
            str += b.ToString("x2");
        }
        return str;
    }
    /***
     * 生成随机数的三种方法
     * 1、Random rd = new Random();int i = rd.Next();
     * 2、Guid.NewGuid().ToString()
     * 3、RNGCryptoServiceProvider csp = new RNGCryptoServiceProvider();byte[] byteCsp = new byte[10];csp.GetBytes(byteCsp);Console.WriteLine(BitConverter.ToString(byteCsp));
     */
    //生成随机数作为盐值
    public static string generateSalt()
    {
        return Guid.NewGuid().ToString();
    }
    public static string Encrypt(string password)
    {
        return md5Encoding(password);
    }
    public static string Encrypt(string password,string salt)
    {
        return md5Encoding(password+salt);
    }
}